# -*- coding:utf-8 -*-
import random
import time

from selenium.webdriver.common.by import By
from PageClass.basePage import BasePage
from Util import logger, ZTCCLOUDError, config


class TimerManagePage(BasePage):

    def __init__(self, driver):
        BasePage.__init__(self, driver)

    # 定时器名称
    _selectTimerName = (By.ID, 'form_remark')

    def input_selectTimerName(self, text):
        self.send_text(text, *self._selectTimerName)
        logger.info(f"输入定时器名字为：{text}")

    # 查询
    _selectButton = (By.XPATH, '//*[@id="form"]/div[3]/div/button[2]')

    def click_selectButton(self):
        self.click(*self._selectButton)
        logger.info("点击查询按钮")

    def click_timerTab(self, timerTab):
        try:
            timerLoc = (By.ID, eval(f'config.interfaceConf.{timerTab}'))
        except Exception:
            raise ZTCCLOUDError(f'定时器tab中未找到 {timerTab}')
        self.click(*timerLoc)
        logger.info("点击共享中心TimerTab")

    # 运行定时器
    _runTimer = (By.XPATH, '//*[@id="app"]//div/div/div[4]/div[2]')

    def click_runTimer(self, n=1):
        if n > 3:
            return False
        self.click(*self._runTimer)
        logger.info(f"第{n}次运行定时器")
        try:
            content = self.getBoxMessage2()
        except Exception as e:
            self.click_runTimer(n + 1)
        else:
            if '运行成功' not in content:
                time.sleep(random.random())
                return self.click_runTimer(n + 1)
            logger.info(f"定时器运行结果：{content}")
            return True
